package com.zjdiepu.www.manage.api.yw;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.zjdiepu.www.base.BaseController;
import com.zjdiepu.www.base.annotation.Update;
import com.zjdiepu.www.base.core.AbstractSysUser;
import com.zjdiepu.www.base.core.ApiResponse;
import com.zjdiepu.www.base.core.PageVo;
import com.zjdiepu.www.base.enums.status.BusiStatus;
import com.zjdiepu.www.base.enums.type.SortFieldType;
import com.zjdiepu.www.base.enums.type.TypeData;
import com.zjdiepu.www.common.util.CollectionInitUtil;
import com.zjdiepu.www.commpent.mapper.yw.YwBusinessVoMapper;
import com.zjdiepu.www.ibiz.dz.IDzLoanMiddleAuditService;
import com.zjdiepu.www.ibiz.yw.IYwBusinessBuyerService;
import com.zjdiepu.www.ibiz.yw.IYwBusinessService;
import com.zjdiepu.www.model.base.dto.SysUser;
import com.zjdiepu.www.model.yw.dto.YwBusiness;
import com.zjdiepu.www.model.yw.request.YwBusinessQueryRequest;
import com.zjdiepu.www.util.YwExtHelper;

@RestController
@RequestMapping(value= "OpenCardApplyController", produces="application/json;charset=UTF-8")
@Api(tags = {"开卡申请"}, produces="application/json;charset=UTF-8")
public class OpenCardApplyController extends BaseController{

	@Resource
	private IYwBusinessService ywBusinessService;
	@Resource
    private YwBusinessVoMapper ywBusinessVoMapper;
	@Resource
	private IDzLoanMiddleAuditService dzLoanMiddleAuditService;
	@Resource
	private IYwBusinessBuyerService ywBusinessBuyerService;
	
	
	@ApiOperation(value = "保存业务开卡提交时间",response = YwBusiness.class, httpMethod="POST", tags={"多表查询"})
    @RequestMapping(value = "modify", method = RequestMethod.POST)
	public ApiResponse modify(@Validated(Update.class) @ModelAttribute YwBusiness ywBusiness){
		AbstractSysUser currentUser = getCurrentUser();
		ywBusinessService.modifyAll(ywBusiness,currentUser);
		return ApiResponse.success(true);
	}
	
	@ApiOperation(value = "条件查询业务信息",response = YwBusiness.class, httpMethod="POST", tags={"多表查询"})
    @RequestMapping(value = "multiList", method = RequestMethod.POST)
    public ApiResponse multiList(@Validated @ModelAttribute YwBusinessQueryRequest ywBusinessQueryRequest){
    	SysUser currentUser = (SysUser)getCurrentUser();
		PageVo page = getPage(ywBusinessQueryRequest);
		ywBusinessQueryRequest.setBaseWhere();
		ywBusinessQueryRequest.setBankLoan(currentUser.getLoanBank());
        YwExtHelper.initExt(ywBusinessQueryRequest, SortFieldType.SUBMIT_TIME);
        ywBusinessQueryRequest.setBusiStatusLess(BusiStatus.CHU_SHEN_DAN_CHU_SHEN_ZHONG.getCode());
        ywBusinessQueryRequest.setTimeApplyOpencardFlag(false);
        ywBusinessQueryRequest.setCodeUnits(currentUser.getCodeUnits());
        ywBusinessQueryRequest.setNeedLimit(true);
        ywBusinessQueryRequest.setTypeData(TypeData.SYSTEM.getCode());
        int records = ywBusinessService.countMultiByExt(ywBusinessQueryRequest);
        page.setRecords(records);
        if (records > 0) {
            List<YwBusiness> ywBusinesss = ywBusinessService.selectMultiByExt(ywBusinessQueryRequest);
            ywBusinessVoMapper.mapper(ywBusinesss);
            for(YwBusiness ywBusines : ywBusinesss) {
            	String operatorId = ywBusines.getOperatorId();
            	if(StringUtils.isNotBlank(operatorId)) {
            		ywBusines.setLock(currentUser.getId().equals(operatorId) ? 1 : 2);
            	} else {
            		ywBusines.setLock(0);
            	}
            }
            page.setRows(ywBusinesss);
        } else {
            page.setRows(CollectionInitUtil.emptyList());
        }
        return ApiResponse.success(page);
	}
}
